package com.yld.car.market;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import com.lidroid.xutils.BitmapUtils;
import com.yld.car.common.ConstantUtils;
import com.yld.car.common.NetworkProgressUtils;
import com.yld.car.domain.DealerBaseInfo;
import com.yld.car.domain.UserInfo;

import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;

public class DealerPageActivity extends BaseActivity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);

		requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
		setContentView(R.layout.dealer_market_base_list);
		getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.title);

		TextView titleTextView = ((TextView) findViewById(R.id.title));
		titleTextView.setText("经销商个人主页");
		utils = NetworkProgressUtils.getInstance();
		showBackButton();
		this.progressBar = ((ProgressBar) findViewById(R.id.progressBar));
		this.progressBar.setVisibility(View.VISIBLE);
		initViews();
		list1 = (ListView) findViewById(R.id.list1);
		Intent intent = getIntent();
		String id = intent.getStringExtra("bId");

		// /System.out.println(id);
		new DealerInfoTask().execute(id);
		list2 = (ListView) findViewById(R.id.list2);
	}

	private ProgressBar progressBar;

	private NetworkProgressUtils utils;
	private ListView list1;
	private ListView list2;

	private class DealerInfoTask extends
			AsyncTask<String, String, DealerBaseInfo> {

		@Override
		protected DealerBaseInfo doInBackground(String... params) {

			DealerBaseInfo d = null;
			if (utils.getAPNType(getApplicationContext()) == -1) {

				return null;
			} else {

				HashMap<String, String> allParams = new HashMap<String, String>();

				UserInfo u = (UserInfo) readObject("userInfo");
				allParams.put("dealerId", params[0]);
				allParams.put("nowUserId", u.getUserId());
				Object o = utils.getWebservicesByData(ConstantUtils.NAME_SPACE,
						ConstantUtils.ALL_METHROD_NAME.get(5),
						ConstantUtils.NAME_SPACE
								+ ConstantUtils.ALL_METHROD_NAME.get(5),
						ConstantUtils.GET_DEALER_BY_DEALER_ID_URL, allParams);

				d = utils.parseBaseDealerInfo(o.toString(), false);
				System.out.println(d);

				return d;

			}

		}

		@Override
		protected void onPreExecute() {

			progressBar.setVisibility(View.VISIBLE);
			super.onPreExecute();
		}

		@Override
		protected void onPostExecute(DealerBaseInfo result) {
			// TODO Auto-generated method stub
			super.onPostExecute(result);

			progressBar.setVisibility(View.GONE);
			if (result == null) {

				Toast.makeText(getApplicationContext(), "网络未连接，请设置网络!",
						Toast.LENGTH_SHORT).show();
			} else {

				DealerInfoAdapter adapter = new DealerInfoAdapter(result);
				list1.setAdapter(adapter);
			}

		}

	}

	private ArrayList<HashMap<String, String>> allTypes = new ArrayList<HashMap<String, String>>();

	private void initViews() {

		/**
		 * 公司形象照 公司名称 公司所在省市 公司所在港口 公司地址 公司简介
		 * 
		 * 经销商负责人 经销商电话
		 */
		HashMap<String, String> img = new HashMap<String, String>();
		img.put("title", "compimg");
		img.put("name", "公司形象照");

		allTypes.add(img);

		HashMap<String, String> compName = new HashMap<String, String>();
		compName.put("title", "compName");
		compName.put("name", "公司名称");

		allTypes.add(compName);
		HashMap<String, String> compProvinceCity = new HashMap<String, String>();

		compProvinceCity.put("title", "compProvinceCity");
		compProvinceCity.put("name", "公司所在省市");
		allTypes.add(compProvinceCity);

		// 公司所在港口 公司地址 公司简介

		HashMap<String, String> compPort = new HashMap<String, String>();

		compPort.put("title", "compPort");
		compPort.put("name", "公司所在港口");

		allTypes.add(compPort);
		HashMap<String, String> compAddress = new HashMap<String, String>();
		compAddress.put("title", "compAddress");
		compAddress.put("name", "公司地址");

		allTypes.add(compAddress);
		HashMap<String, String> compIntro = new HashMap<String, String>();
		compIntro.put("title", "compIntro");
		compIntro.put("name", "公司简介");

		allTypes.add(compIntro);
		// 经销商负责人 经销商电话
		HashMap<String, String> compPeople = new HashMap<String, String>();
		compPeople.put("title", "compPeople");
		compPeople.put("name", "经销商负责人");

		allTypes.add(compPeople);
		HashMap<String, String> telephone = new HashMap<String, String>();
		telephone.put("title", "telephone");
		telephone.put("name", "经销商电话");

		allTypes.add(telephone);

	}

	private class DealerInfoAdapter extends BaseAdapter {

		private DealerBaseInfo info;

		public DealerInfoAdapter(DealerBaseInfo info) {

			this.info = info;

		}

		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			return allTypes.size();
		}

		@Override
		public Object getItem(int position) {
			// TODO Auto-generated method stub
			return allTypes.get(position);
		}

		@Override
		public long getItemId(int position) {
			// TODO Auto-generated method stub
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {

			Map<String, String> hashMap = allTypes.get(position);

			String s = hashMap.get("title");

			convertView = LayoutInflater.from(getApplicationContext()).inflate(
					R.layout.dealer_base_info_others, null);
			if ("compimg".equals(s)) {
				convertView = LayoutInflater.from(getApplicationContext())
						.inflate(R.layout.dealer_base_info_pic, null);
				TextView textView = (TextView) convertView
						.findViewById(R.id.itemTitle);
				textView.setText(hashMap.get("name"));
				ImageView image = (ImageView) convertView
						.findViewById(R.id.itemPic1);

				String downloadUrl = "";
				String tmp = info.getUserimg();
				if (tmp.indexOf("aodi") >= 0) {

					downloadUrl = ConstantUtils.DOWNLOAD_IMAGE_URL_NEW + tmp;
				} else {
					downloadUrl = ConstantUtils.DOWNLOAD_IMAGE_URL + tmp;
				}

				BitmapUtils bitmapUtils = new BitmapUtils(
						getApplicationContext());

				// 加载网络图片
				bitmapUtils.display(image, downloadUrl);
				// image.setImageBitmap(mBitmap);
			}
			if ("compName".equals(s)) {

				TextView textView = (TextView) convertView
						.findViewById(R.id.itemTitle);
				textView.setText(hashMap.get("name"));
				TextView content = (TextView) convertView
						.findViewById(R.id.itemContent);
				content.setText(info.getCompname());
			}
			if ("compProvinceCity".equals(s)) {

				TextView textView = (TextView) convertView
						.findViewById(R.id.itemTitle);
				textView.setText(hashMap.get("name"));
				TextView content = (TextView) convertView
						.findViewById(R.id.itemContent);
				content.setText(info.getProvincecity());
			}
			if ("compPort".equals(s)) {

				TextView textView = (TextView) convertView
						.findViewById(R.id.itemTitle);
				textView.setText(hashMap.get("name"));
				TextView content = (TextView) convertView
						.findViewById(R.id.itemContent);
				content.setText(info.getPort());
			}
			if ("compAddress".equals(s)) {

				TextView textView = (TextView) convertView
						.findViewById(R.id.itemTitle);
				textView.setText(hashMap.get("name"));
				TextView content = (TextView) convertView
						.findViewById(R.id.itemContent);
				content.setText(info.getAddress());
			}
			if ("compIntro".equals(s)) {

				TextView textView = (TextView) convertView
						.findViewById(R.id.itemTitle);
				textView.setText(hashMap.get("name"));
				TextView content = (TextView) convertView
						.findViewById(R.id.itemContent);
				content.setText(info.getIntro());
			}
			if ("compPeople".equals(s)) {

				TextView textView = (TextView) convertView
						.findViewById(R.id.itemTitle);
				textView.setText(hashMap.get("name"));
				TextView content = (TextView) convertView
						.findViewById(R.id.itemContent);
				content.setText(info.getComppeople());
			}

			if ("telephone".equals(s)) {

				TextView textView = (TextView) convertView
						.findViewById(R.id.itemTitle);
				textView.setText(hashMap.get("name"));
				TextView content = (TextView) convertView
						.findViewById(R.id.itemContent);
				content.setText(info.getTelephone());
			}

			return convertView;
		}

		@Override
		public boolean isEnabled(int position) {

			Map<String, String> hashMap = allTypes.get(position);

			String s = hashMap.get("title");
			if ("compimg".equals(s)) {
				return super.isEnabled(position);
			}
			return false;
			// return super.isEnabled(position);
		}

	}

}
